#mapping-script
# -*- coding: ms949 -*-
import bots.transform as transform
import bots.usersys.mappings.utils as utils

def main(inn,out):
    #XML
    ns1 = '{urn:kr:gov:kcs:data:standard:KCS_AmendmentOfReleaseOrCarryInOfContainerSchemaModule:1:0:0}'
    ns2 = '{urn:kr:gov:kcs:common:standard:KCS_ReusableAggregateBusinessInformationEntitySchemaModule:1:0:0}'
    rootelement = ns1 + 'AmendmentOfReleaseOrCarryInOfContainer'
    
    #Read from inn by EDIFACT order
    DocumentTypeCode = inn.get({'BOTSID':'UNH'},{'BOTSID':'BGM','C002.1001':None})
    PreviousCustomsDocumentReferenceID = inn.get({'BOTSID':'UNH'},{'BOTSID':'BGM','1004':None})
    
    ReleaseCarryInDateTime = inn.get({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'150','C507.2380':None})
    DocumentIssueDate = inn.get({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'141','C507.2380':None})
    
    AmendmentReason = inn.get({'BOTSID':'UNH'},{'BOTSID':'FTX','4451':'ACB','C108.4440#1':None})
    AmendmentReason2 = inn.get({'BOTSID':'UNH'},{'BOTSID':'FTX','4451':'ACB','C108.4440#2':None})
    if AmendmentReason2 != None: AmendmentReason = AmendmentReason + ' ' + AmendmentReason2
    
    CustomsOfficeCodeID = inn.get({'BOTSID':'UNH'},{'BOTSID':'LOC','C517.1131':'113','C517.3225':None})
    CustomsOfficeCode = inn.get({'BOTSID':'UNH'},{'BOTSID':'LOC','C519.1131':'5AB','C519.3223':None})
    
    AmendmentTypeCode = inn.get({'BOTSID':'UNH'},{'BOTSID':'GIS','C529.1131':'5AI','C529.7365':None})  
    
    out.ta_info['botskey'] = 'CUSCAR-5LF-' + PreviousCustomsDocumentReferenceID.strip()
    
    #0~99 Loop
    for gid in inn.getloop({'BOTSID':'UNH'},{'BOTSID':'GID'}):
        AmendmentContentSequenceID = gid.get({'BOTSID':'GID','C213#1.7224':None})
        AmendmentContentItemCode = gid.get({'BOTSID':'GID','C213#1.7065':None})
    
        UCRIdentificationID = gid.get({'BOTSID':'GID'},{'BOTSID':'FTX','4451':'ADE','C108.4440#1':None})
        UCRMasterBLSequenceID = gid.get({'BOTSID':'GID'},{'BOTSID':'FTX','4451':'ADE','C108.4440#2':None})
        UCRHouseBLSequenceID = gid.get({'BOTSID':'GID'},{'BOTSID':'FTX','4451':'ADE','C108.4440#3':None})
        
        AmendmentContentBeforeDescription = gid.get({'BOTSID':'GID'},{'BOTSID':'FTX','4451':'AAO','C108.4440#1':None})
        AmendmentContentAfterDescription = gid.get({'BOTSID':'GID'},{'BOTSID':'FTX','4451':'CHG','C108.4440#1':None})
        
        lou = out.putloop({'BOTSID':rootelement,'BOTSID':ns1+'AmendmentContent'})
        lou.put({'BOTSID':ns1+'AmendmentContent',ns2+'SequenceID':AmendmentContentSequenceID})
        lou.put({'BOTSID':ns1+'AmendmentContent',ns2+'AmendmentItemCode':AmendmentContentItemCode})
        lou.put({'BOTSID':ns1+'AmendmentContent',ns2+'BeforeDescription':AmendmentContentBeforeDescription})
        lou.put({'BOTSID':ns1+'AmendmentContent',ns2+'AfterDescription':AmendmentContentAfterDescription})
        lou.put({'BOTSID':ns1+'AmendmentContent'},{'BOTSID':ns2+'UCR',ns2+'IdentificationID':UCRIdentificationID})
        lou.put({'BOTSID':ns1+'AmendmentContent'},{'BOTSID':ns2+'UCR',ns2+'MasterBLSequenceID':UCRMasterBLSequenceID})
        lou.put({'BOTSID':ns1+'AmendmentContent'},{'BOTSID':ns2+'UCR',ns2+'HouseBLSequenceID':UCRHouseBLSequenceID})      
    #Loop End
    
    AmendmentGradeNumeric = inn.get({'BOTSID':'UNH'},{'BOTSID':'CNT','C270.6066':None})
          
    #Write to out by XML order
    out.put({'BOTSID':rootelement,ns1+'ReleaseCarryInDateTime':utils.isoDateTimeFormat(ReleaseCarryInDateTime)})
    
    out.put({'BOTSID':rootelement,ns1+'AmendmentReason':AmendmentReason})
    out.put({'BOTSID':rootelement,ns1+'AmendmentTypeCode':AmendmentTypeCode})
    out.put({'BOTSID':rootelement,ns1+'AmendmentGradeNumeric':AmendmentGradeNumeric})
    
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'TypeCode':DocumentTypeCode})
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'IssueDate':utils.isoDateFormat(DocumentIssueDate)})
    
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'PreviousCustomsDocument',ns2+'referenceID':PreviousCustomsDocumentReferenceID})
    
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'CustomsOffice',ns2+'OfficeCode':CustomsOfficeCode})
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'CustomsOffice',ns2+'CodeID':CustomsOfficeCodeID})
    
    out.put({'BOTSID':rootelement,ns1+'AmendmentOfReleaseOrCarryInOfContainer__{http://www.w3.org/2001/XMLSchema-instance}schemaLocation':'urn:kr:gov:kcs:data:standard:KCS_AmendmentOfReleaseOrCarryInOfContainerSchemaModule:1:0:0 ./schema/uncefact/data/KCS_AmendmentOfReleaseOrCarryInOfContainerSchemaModule_1.0.0_standard.xsd'})
    

